//
//  RequestManager.swift
//  sxwb
//
//  Created by TianDu on 2016/11/16.
//  Copyright © 2016年 TianDu. All rights reserved.
//

import AFNetworking

enum requestType : Int {
    case Get  = 0
    case Post = 1
}

class RequestManager: AFHTTPSessionManager {
    //通过调用share来创建的实例,都是单例
    static let share : RequestManager = {
        let manager = RequestManager()
        manager.responseSerializer.acceptableContentTypes = ["application/json", "text/json", "text/javascript","text/html","text/plain"]
        return manager
    }()
    
    
    func request(_ type:requestType , url:String, parameters: [String:Any],finished :@escaping (_ result:Any?,_ error:Error?)->()){
        
        let successCallBack = { (task:URLSessionDataTask,result:Any?) in
            finished(result,nil)
        }
        
        let failureCallBack = { (task:URLSessionDataTask?,error:Error) in
            finished(nil,error)
        }
        
        if type == .Get {
            
            get(url, parameters: parameters, progress: nil, success: successCallBack, failure: failureCallBack)
        }else{
            post(url, parameters: parameters, progress: nil, success: successCallBack, failure: failureCallBack)
        }
    }
}
